#!/usr/bin/env python

import random
import os

def createZipf(nodes, services, outputFilename):
	result = [[] for i in range(len(nodes))]
	minProb = 1./len(nodes)
	for i in range(1, services[-1] + 1):
		prob = 1./i
		selectedNodes = random.sample(nodes, int(round(len(nodes)*prob)) if prob > minProb else 1)
		selectedService = random.choice(services)
		services.remove(selectedService)
		for j in selectedNodes:
			result[j].append(selectedService)
	writeToFile(result, outputFilename)
    
def writeToFile(nodes, fileName):
	for i, node  in enumerate(nodes, start=1):
		with open(fileName + str(i) + ".txt", 'w') as f:
			for service in node:
				f.write("{}\n".format(service))

if __name__ == '__main__':
	random.seed(int(os.getenv('rand_seed')))
	nodes = range(int(os.getenv('total_nodes')))
	services = range(1, int(os.getenv('nr_services')) + 1)
	createZipf(nodes, services, os.getenv('filename'))